﻿/**
* ...
* @author Default
* @version 0.1
*/

package  {
	
	
	import flash.display.*;
	import flash.filters.*;
	import flash.display.Stage;
	import flash.events.*;
	import org.papervision3d.core.geom.*;
	import org.papervision3d.objects.lines.Line3DSegment;

	
	
	// Import Papervision3D
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.scenes.*;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.materials.*;
	import org.papervision3d.utils.*;
	import org.papervision3d.objects.*;
	import flash.text.TextField;
	import flash.utils.*;
	
	public class Main extends Sprite{
		var container :Sprite;
		var scene     :MovieScene3D;
		var camera    :Camera3D;
		var time	  :Number;
		var lines	  :Line3D;

		
		var i:int=0;
	   var x0:Number,y0:Number,z0:Number,x1:Number,y1:Number,z1:Number;
	   var h:Number;
	   var a:Number;
	   var b:Number;
	   var c:Number;
	   var N:int;
		var camTarget:DisplayObject3D;
		var zoomIn:Boolean = false;
		var zoomOut:Boolean = false;
		private var starSprite:Sprite;
		private var vParticles:VertexParticles;
		private var particleField:ParticleField;
		
		
		public function Main() {
			init();
			
			
		}
		
		public function addVertex(){
			

		
		}
		
		public function init():void {
				

			stage.scaleMode = "noScale"
			
			init3D();
			
			swapChildren(container, time_txt);
		
			time = getTimer();
			
			addEventListener( Event.ENTER_FRAME, loop );
			controls.redraw.addEventListener(MouseEvent.CLICK, resetLorenz);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPress);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyRelease);

			
		}
		
		public function resetLorenz(e:MouseEvent){
		
			   x0 = 0.1;
			   y0 = 0;
			   z0 = 0;
			   h = 0.01;
			   a = controls.chk_a.value;
			   b = controls.chk_b.value;
			   c = controls.chk_c.value;
			   N = 3000;
			   i = 0;
			   lines.geometry.vertices = new Array();
			   lines.segments = new Array();

		
		}
		
		public function keyPress(e:KeyboardEvent){
			
			if(e.charCode == 61)
			{
				zoomIn = true;
				zoomOut = false;
			}else if(e.charCode == 45){
				
				zoomIn = false;
				zoomOut = true;
			}
		}
		
		public function keyRelease(e:KeyboardEvent){
			
			if(e.charCode == 61)
			{
				zoomIn = false;
				
			}else if(e.charCode == 45){
				
				
				zoomOut = false;
			}
		}
		
		public function init3D():void {
			
			// Create container sprite and center it in the stage
			container = new Sprite();

			addChild( container );
			
			container.x = 300;
			container.y = 200;

			lines = new Line3D([new Vertex3D(0,0,0)], 0x751A1D, 1, 2, false);
			
			scene = new MovieScene3D(container);
			scene.addChild(lines);
			
			lines.container.filters = [new BlurFilter(), new GlowFilter(0xC4C19A)];

			
			
			camera = new Camera3D();
			camera.zoom = 48;
			camera.z = -100;
			
			camTarget = new DisplayObject3D();
			camera.target = camTarget;
			
			 
			resetLorenz(null);

		}
			
		// ___________________________________________________________________ Loop
		
		function loop(event:Event):void 
		{
			
			if(zoomIn){
				
				
				if(camera.zoom <= 140)
					camera.zoom += 0.5;
			
			}else if(zoomOut){
				
				if(camera.zoom >= 20)
					camera.zoom -= 0.5;
			}

			
			if(++i < N ){
			  
			
			  x1 = x0 + h * a * (y0 - x0);
			  y1 = y0 + h * (x0 * (b - z0) - y0);
			  z1 = z0 + h * (x0 * y0 - c * z0);
			  
			  camTarget.x +=  (x1-x0)/2;
			  camTarget.y +=  (y1-y0)/2;
		 	  camTarget.z +=  (z1-z0)/2;
			  //lines.color = 0x75<<16 | ((i/N*0x1A)<<8) |  i/N*0x1D;

			  x0 = x1;
			  y0 = y1;
			  z0 = z1;
				
			  var v:Vertex3D = new Vertex3D(x1, y1, z1);
			  lines.addVertex(v);
			
			}
			
			
			camera.hover(0, (container.x - mouseX)/30, (container.y-mouseY)/30);
			
			scene.renderCamera( this.camera );
			//lines_txt.text = (lines.geometry.vertices.length*3)+" lines";
			time_txt.text = (getTimer()-time)+" ms";
			time = getTimer();

		}
	}
	
}
